using System;
using System.Collections.Generic;
using System.Text;

using RecruitmentSystem.Common;
using RecruitmentSystem.DataAccess;

namespace RecruitmentSystem.Business
{
    public class JobManager
    {
        private JobAccessor jobAccessor;

        public JobManager()
        {
            jobAccessor = new JobAccessor();
        }

        public bool CreateJob(JobPosition position)
        {
            bool result = false;

            try
            {
                result = jobAccessor.AddPosition(position);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in create job ", ex);
            }

            return result;
        }

        public bool ChangeJobById(JobPosition position)
        {
            bool result = false;

            try
            {
                result = jobAccessor.UpdatePositionById(position);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in change job", ex);
            }

            return result;
        }

        public bool ChangeJobStatus(int positionId, PositionStatus positionType)
        {
            bool result = false;

            try
            {
                result = jobAccessor.UpdatePositionStatus(positionId, positionType);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in update job status", ex);
            }

            return result;
        }

        public bool ChangeeJobStatus(List<int> ids, PositionStatus positionType)
        {
            bool result = false;

            if (ids != null)
            {
                try
                {
                    foreach (int id in ids)
                    {
                        ChangeJobStatus(id, positionType);
                    }

                    result = true;
                }
                catch (Exception ex)
                {
                    Logger.Error("Exception occurs in disable jobs", ex);
                }
            }

            return result;
        }

        public bool RemoveJob(int id)
        {
            bool result = false;

            try
            {
                result = jobAccessor.DeletePosition(id);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in remove job", ex);
            }

            return result;
        }

        public bool RemoveJobs(List<int> ids)
        {
            bool result = false;

            if (ids != null && ids.Count > 0)
            {
                try
                {
                    foreach (int id in ids)
                    {
                        RemoveJob(id);
                    }

                    result = true;
                }
                catch (Exception ex)
                {
                    Logger.Error("Exception occurs in remove jobs", ex);
                }
            }

            return result;
        }

        public List<JobPosition> GetJobsById(int userId,
            UserType userType, PositionStatus positionType,int pagesize,int pagecurrent)
        {
            List<JobPosition> positions = new List<JobPosition>();

            try
            {
                switch (userType)
                {
                    case UserType.AdminUser:
                        positions = jobAccessor.GetPositionsByAdminUserId(userId,
                            positionType, pagesize, pagecurrent);
                        break;
                    case UserType.EnterpriseUser:
                        positions = jobAccessor.GetPositionsByEnterpriseUserId(userId,
                            positionType, pagesize, pagecurrent);
                        break;
                    case UserType.PersonalUser:
                        positions = jobAccessor.GetPositionsByPersonalUserId(userId,
                            positionType, pagesize, pagecurrent);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get all positions", ex);
            }

            return positions;
        }

        public int GetAllJobsById(int userId, UserType userType, PositionStatus positionType)
        {
            int count = 0;

            try
            {
                switch(userType)
                {
                    case UserType.AdminUser:
                        count = jobAccessor.GetJobsCountByAdminUserId(userId, positionType);
                        break;
                    case UserType.EnterpriseUser:
                        count = jobAccessor.GetJobsCountByEnterpriseUserId(userId, positionType);
                        break;
                    case UserType.PersonalUser:
                        count = jobAccessor.GetJobsCountByPersonalUserId(userId, positionType);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get all jobs", ex);
            }

            return count;
        }

        public JobPosition GetJob(int jobId)
        {
            JobPosition position = null;

            try
            {
                position = jobAccessor.GetPosition(jobId);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get job", ex);
            }

            return position;
        }

        public bool ApplyJob(int jobId, int userId, UserType userType)
        {
            bool result = false;

            try
            {
                result = jobAccessor.ApplyPosition(jobId, userId, userType);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in apply job", ex);
            }

            return result;
        }

        public List<JobPosition> SearchJobs(string positionType, string workProvince,
            string workDistrict, string salary, string genderRequired, string workMethod,
            string title, string enterpriseType, string category, int pageCurrent, int pageSize)
        {
            List<JobPosition> positions = new List<JobPosition>();

            try
            {
                positions = jobAccessor.GetPositions(positionType, 
                    workProvince, workDistrict, salary, genderRequired, workMethod, 
                    title, enterpriseType, category, pageCurrent, pageSize);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in search jobs", ex);
            }

            return positions;
        }

        public List<Link> GetAllLink()
        {
            List<Link> links = new List<Link>();

            try
            {
                links = jobAccessor.GetLinks();
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get all link", ex);
            }

            return links;
        }

        public bool CreateLink(Link link)
        {
            bool result = false;

            try
            {
                result = jobAccessor.AddLink(link);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in create link", ex);
            }

            return result;
        }

        public bool ChangeLink(Link link)
        {
            bool result = false;

            try
            {
                result = jobAccessor.UpdateLink(link);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in change link", ex);
            }

            return result;
        }

        public bool DeleteLink(int linkId)
        {
            bool result = false;

            try
            {
                result = jobAccessor.RemoveLink(linkId);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in delete link", ex);
            }

            return result;
        }

        public int GetPositionsCount(string positionType, string workProvince,
            string workDistrict, string salary, string genderRequired, string workMethod,
            string title, string enterpriseType, string category)
        {
            int count = 0;

            try
            {
                count = jobAccessor.GetPositionsCount(positionType, workProvince,
                    workDistrict, salary, genderRequired, workMethod,
                    title, enterpriseType, category);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get positions count", ex);
            }

            return count;
        }

        public List<UserPosition> GetUserPositions(string name,
            string enterpriseName, string positionName, int pageCurrent, int pageSize, UserType type)
        {
            List<UserPosition> positions = new List<UserPosition>();

            try
            {
                positions = jobAccessor.GetUserPositions(name,
                    enterpriseName, positionName, pageCurrent, pageSize);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get users' positions", ex);
            }

            return positions;
        }

        public int GetUserPositionsCount(string name,
            string enterpriseName, string positionName)
        {
            int count = 0;

            try
            {
                count = jobAccessor.GetUserPositionsCount(name, enterpriseName, positionName);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get users' positions count", ex);
            }

            return count;
        }
    }
}
